package com.hellobike.haxing.modules.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.order.entity.OrderShopReward;
import com.hellobike.haxing.modules.order.service.IOrderShopRewardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @Description: 团长分享下单奖励
 * @Author: haxing-boot
 * @Date: 2020-11-16
 * @Version: V1.0
 */
@RestController
@RequestMapping("/order/orderShopReward")
@Slf4j
public class OrderShopRewardController extends BaseController<OrderShopReward, IOrderShopRewardService> {
    @Autowired
    private IOrderShopRewardService orderShopRewardService;


    @PostMapping(value = "/info")
    public Result<?> info() {
        OrderShopReward orderShopReward = orderShopRewardService.getOne(new QueryWrapper<OrderShopReward>().lambda().eq(OrderShopReward::getDeleted, 0)
                .orderByDesc(OrderShopReward::getId).last("limit 1"));
        return Result.ok(orderShopReward);
    }

    /**
     * 分页列表查询
     *
     * @param orderShopReward
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "团长分享下单奖励-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(OrderShopReward orderShopReward,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<OrderShopReward> queryWrapper = QueryGenerator.initQueryWrapper(orderShopReward, req.getParameterMap());
        Page<OrderShopReward> page = new Page<OrderShopReward>(pageNo, pageSize);
        IPage<OrderShopReward> pageList = orderShopRewardService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param orderShopReward
     * @return
     */
    @AutoLog(value = "团长分享下单奖励-添加", operateType = 2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody OrderShopReward orderShopReward) {
        orderShopRewardService.save(orderShopReward);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param orderShopReward
     * @return
     */
    @AutoLog(value = "团长分享下单奖励-编辑", operateType = 3)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody OrderShopReward orderShopReward) {
        if (orderShopReward.getStepTwoLimit() == null) {
            orderShopReward.setStepTwoLimit(0);
        }
        if (orderShopReward.getStepTwoReward() == null) {
            orderShopReward.setStepTwoReward(BigDecimal.ZERO);
        }
        if (orderShopReward.getStepThreeLimit() == null) {
            orderShopReward.setStepThreeLimit(0);
        }
        if (orderShopReward.getStepThreeReward() == null) {
            orderShopReward.setStepThreeReward(BigDecimal.ZERO);
        }
        OrderShopReward result = orderShopRewardService.getOne(new QueryWrapper<OrderShopReward>().lambda().eq(OrderShopReward::getDeleted, 0)
                .orderByDesc(OrderShopReward::getId).last("limit 1"));
        if (result == null) {
            orderShopRewardService.save(orderShopReward);
        } else {
            orderShopReward.setId(result.getId());
            orderShopRewardService.updateById(orderShopReward);
        }
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "团长分享下单奖励-删除", operateType = 4)
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Long id) {
        orderShopRewardService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "团长分享下单奖励-批量删除", operateType = 4)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.orderShopRewardService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
        OrderShopReward orderShopReward = orderShopRewardService.getById(id);
        if (orderShopReward == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(orderShopReward);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param orderShopReward
     */
    @AutoLog(value = "团长分享下单奖励-导出excel", operateType = 6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OrderShopReward orderShopReward) {
        return super.exportXls(request, orderShopReward, OrderShopReward.class, "团长分享下单奖励");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @AutoLog(value = "团长分享下单奖励-导入excel", operateType = 5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OrderShopReward.class);
    }

}
